package opc_service

import (
	"beta/pkg/e"
	"beta/pkg/logger"
	"beta/pkg/util"
	"github.com/tidwall/gjson"
	"time"
)

type SimulateDeduct struct {
	IdentityID     int      `json:"identityId"`
	ProductIDList  []string `json:"productIdList"`
	Amount         string   `json:"amount"`
	BalancePayRule `json:"balancePayRule"`
}

type BalancePayRule struct {
	PayStrategy string `json:"payStrategy"`
}

func (opc OpcService) GetSimulateDeduct(productId string, identityId int) (int, error) {
	host := opc.Hosts[appName].AppDomain
	url := host + "/opc/v1/pay/simulateDeduct"
	productIdList := []string{productId}
	balancePayRule := BalancePayRule{PayStrategy: "1"}

	deduct := SimulateDeduct{
		IdentityID:     identityId,
		ProductIDList:  productIdList,
		Amount:         "1.0",
		BalancePayRule: balancePayRule,
	}
	
	logger.ZapLogger.Infow("【模拟扣款】", "请求参数", deduct, "traceId", opc.TraceId)
	cookie := util.GetCookie(util.BossCookieName, opc.Token)
	httpClient := util.NewHttpClientWithCookie(3 * time.Second, cookie).R()
	resp, respErr := httpClient.SetHeaders(e.GetHeaderMap(e.JsonHeader)).
		SetBody(deduct).Post(url)

	logger.ZapLogger.Infow("【模拟扣款】", "请求结果", resp, "traceId", opc.TraceId)

	if respErr != nil {
		return 0, respErr
	}
	couponList := gjson.GetBytes(resp.Body(), "data.couponList")
	couponId := gjson.Get(couponList.Array()[0].String(), "couponId").Int()

	return int(couponId), nil

}